// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.chimi.service.impl;

import com.liuxinlong.common.AosException;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.chimi.service.BarPlateService;
import com.liuxinlong.modules.dao.BarCardDao;
import com.liuxinlong.modules.dao.BarPlateDao;
import com.liuxinlong.modules.entity.BarPlate;
import com.liuxinlong.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 论坛板块管理服务层接口实现
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-07-07
 */
@Service
@Slf4j
public class BarPlateServiceImpl implements BarPlateService {

    @Autowired
    private BarPlateDao barPlateDao;

    @Autowired
    private BarCardDao barCardDao;

    @Override
    public List<Map<String, Object>> pagePlate(Map<String, Object> queryParam) {
        int startNum = (int) queryParam.get("startNum");
        List<BarPlate> originalList = barPlateDao.pageBarPlateList(queryParam);
        return completeResult(originalList,startNum);
    }

    @Override
    public int getPlateCount(Map<String, Object> queryParam) {
        return barPlateDao.getBarPlateCount(queryParam);
    }

    @Override
    public void addPlate(BarPlate barPlate) {
        barPlate.setId(SnowFlake.nextIdStr());
        barPlateDao.insert(barPlate);
    }

    @Override
    public void updatePlate(BarPlate barPlate) {
        String id = barPlate.getId();
        if (StringUtils.isEmpty(id)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"修改板块信息异常，板块不存在！");
        }
        BarPlate oldInfo = barPlateDao.selectById(id);
        if (ObjectUtils.isEmpty(oldInfo)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"修改板块信息异常，板块不存在！");
        }
        barPlateDao.updateById(barPlate);
    }

    @Override
    public void deletePlate(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"删除板块信息异常，板块不存在！");
        }
        BarPlate oldInfo = barPlateDao.selectById(id);
        if (ObjectUtils.isEmpty(oldInfo)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"删除板块信息异常，板块不存在！");
        }
        if (barCardDao.getCardCountByPlate(id) > 0) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"删除板块信息异常，板块不存在！");
        }
        barPlateDao.deleteById(id);
    }

    /**
     * 数据补全转换
     * @param originList 原始数据列表
     * @param startNum 起始查询数
     * @return 补全数据
     */
    private List<Map<String, Object>> completeResult(List<BarPlate> originList, int startNum) {
        if (ObjectUtils.isEmpty(originList)) {
            return new ArrayList<>();
        }
        List<Map<String, Object>> resultList = new ArrayList<>(originList.size());
        int sort = startNum + 1;
        for (BarPlate record : originList) {
            Map<String, Object> map = ObjectUtils.object2Map(record);
            map.put("sort", sort);
            resultList.add(map);
            sort++;
        }
        return resultList;
    }
}
